# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.4 5.3 5.2 5.1"
_defaultlua="5.4"
pkgname=haserl
pkgver=0.9.36
pkgrel=4
pkgdesc="Html And Shell Embedded Report Language"
url="https://haserl.sourceforge.net/"
arch="all"
license="GPL-2.0-only"
makedepends="automake autoconf"
subpackages="$pkgname-doc"
for _i in $_luaversions; do
	depends="$depends haserl-lua$_i"
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages haserl-lua$_i:_split"
done
options="suid !check" # no test suite
source="https://downloads.sourceforge.net/haserl/haserl-$pkgver.tar.gz"

# secfixes:
#   0.9.36-r0:
#     - CVE-2021-29133

prepare() {
	default_prepare

	sed -i -e 's/^        /\t/' src/Makefile.am
	rm -f src/haserl_lualib.inc

	aclocal && autoconf && autoheader && automake --add-missing

	for _i in $_luaversions; do
		cp -r "$builddir" "$srcdir"/build-$_i
	done
}

build() {
	for _i in $_luaversions; do
		cd "$srcdir"/build-$_i
		./configure \
			--build=$CBUILD \
			--host=$CHOST \
			--prefix=/usr \
			--with-lua=lua$_i
		make
	done
}

package() {
	for _i in $_luaversions; do
		cd "$srcdir"/build-$_i
		make DESTDIR="$pkgdir/" install
		chmod 4111 "$pkgdir"/usr/bin/haserl
		mv "$pkgdir"/usr/bin/haserl "$pkgdir"/usr/bin/haserl-lua$_i
	done
	ln -s haserl-lua$_defaultlua "$pkgdir"/usr/bin/haserl
}

_split() {
	local _ver=${subpkgname#haserl-lua}
	pkgdesc="Html And Shell Embedded Report Language with Lua $_ver support"
	replaces="$pkgname"
	depends=

	amove usr/bin/haserl-lua$_ver
}

sha512sums="727c6b4cf26bb7fd9d55c328dcca47dc0093b2836cd4874ad28a9c07d9ad4c82c22b899f64df33bad37325f66ce1af8aec1fe0a90e42b9f6cc06b01afe3062d9  haserl-0.9.36.tar.gz"
